package com.zheng.study.web.controller.main.company;

import com.zheng.study.company.entity.Company;
import com.zheng.study.company.entity.Department;
import com.zheng.study.company.entity.Duty;
import com.zheng.study.company.entity.User;
import com.zheng.study.company.service.DepartmentService;
import com.zheng.study.web.controller.BaseController;
import com.zheng.study.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门
 * ============================================================================
 * author : dzw
 * createDate:  2018/8/27 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/department")
public class DepartmentController extends BaseController {
	private static final long serialVersionUID = 1379771126936712087L;

	@Autowired
	private DepartmentService departmentService;

	/**
	 * tree
	 */
	@RequestMapping(value = "tree", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public List<Map<String, Object>> tree(@CurrentUser User user, String id){
		Company company = user.getCompany();
		List<Department> departmentList = null;
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		if(StringUtils.isEmpty(id)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", "0");
			map.put("name", company!=null ? company.getName() : "部门信息");
			map.put("isParent", departmentService.isParentDepartment(null));
			mapList.add(map);
			return mapList;
		}else if("0".equals(id)){
			departmentList = departmentService.getRootDepartments(company);
		}else{
			Department department = departmentService.get(id);
			if (department != null) {
				departmentList = departmentService.getDepartments(company, department);
			}
		}

		if (departmentList == null) {
			departmentList = new ArrayList<Department>();
		}

		Map<String, Object> rMap;
		for(Department department:departmentList){
			rMap = new HashMap<String, Object>();
			rMap.put("id", department.getId());
			rMap.put("name", StringUtils.isNotEmpty(department.getName())?department.getName():"");
			rMap.put("isParent", departmentService.isParentDepartment(department));
			mapList.add(rMap);
		}
		return mapList;
	}

	/**
	 * 新建部门dialog
	 */
	@RequestMapping(value = "input", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
	public String input(Model model, String parentId, String keyId){
		if(StringUtils.isNotEmpty(keyId)){
			Department department = departmentService.get(keyId);
			model.addAttribute("department", department);
			if(department!=null && department.getParent()!=null){
				model.addAttribute("parentDept", department.getParent());
			}
		}

		if(!model.containsAttribute("parentDept")){
			System.out.println("没有添加进父及");
			model.addAttribute("parentDept", departmentService.get(parentId));
		}

		return "/main/company/department_input";
	}

	/**
	 * 保存部门
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@CurrentUser User user, String keyId, String parentId, String name, String description){
		if(StringUtils.isBlank(name)){
			return ajaxJson(resultService.fail("请输入部门名称"));
		}

		Department department;
		if(StringUtils.isNotEmpty(keyId)){
			department = departmentService.get(keyId);
		}else{
			department = new Department();
			department.setCreater(user);
			department.setCompany(user.getCompany());
		}
		department.setParent(departmentService.get(parentId));
		department.setName(name);
		department.setDescription(description);

		if(StringUtils.isNotEmpty(keyId)){
			departmentService.update(department);
		}else{
			departmentService.save(department);
		}

		departmentService.updatePath(department);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", department.getId());
		map.put("parentId", department.getParent()!=null ? department.getParent().getId() : "");
		return resultService.successWithData(map);
	}

	/**
	 * 删除部门
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(@CurrentUser User user, String keyId){
		if(StringUtils.isBlank(keyId)){
			return resultService.fail("请选择部门");
		}
		Department department = departmentService.get(keyId);
		return departmentService.deleteDepartment(department);
	}

	/**
	 * 设置部门负责人
	 */
	@RequestMapping(value = "setLeader", method = RequestMethod.POST)
	@ResponseBody
	public Object setLeader(@CurrentUser User user, String departmentId, String dutyId){
		if(StringUtils.isNotBlank(departmentId) && StringUtils.isNotBlank(dutyId)){
			Department department = departmentService.get(departmentId);
			Duty duty = dutyService.get(dutyId);

			if(department.getLeader()!=null && dutyId.equals(department.getLeader().getId())){ //撤销负责人身份
				department.setLeader(null);
			}else{
				department.setLeader(duty);
			}
			departmentService.update(department);
			return ajaxJson(resultService.success("设置成功"));
		}
		return ajaxJson(resultService.fail("设置失败"));
	}

}
